home *** CD-ROM | disk | FTP | other *** search
/ InterCD 2000 December / december_2000.iso / Intercd / root / Mail / ^MailShield / setup.exe / data1.cab / Tcl_Library_Files / upcfg.tcl < prev    next >
Encoding:
Text File  |  2000-10-18  |  4.2 KB  |  152 lines

  1. # this program will merge new MailShield config files
  2. # with old ones. The settings from the old files are 
  3. # read in, along with the help description from the 
  4. # new files, and this is written out to the new directory.
  5.  
  6. # parses out the information from a single config file
  7. proc fix_file { file old_dir new_dir dest_dir } {
  8.  
  9.     #puts "\rProcessing $file"
  10.     
  11.     set old_help {}
  12.     set old_value {}
  13.     set new_help {}
  14.     set new_value {}
  15.     
  16.     if { [file exists "$old_dir/$file"] } {
  17.     
  18.         SplitHelpValueForFile "$old_dir/$file" old_help old_value
  19.         SplitHelpValueForFile "$new_dir/$file" new_help new_value
  20.     
  21.         set dest_file [open "$dest_dir/$file" w]
  22.         puts $dest_file $new_help
  23.         puts $dest_file $old_value
  24.         close $dest_file
  25.     } else {
  26.         # if the old file doesn't exist, then just copy it to the destination directory
  27.         catch { file copy "$new_dir/$file" "$dest_dir/$file" }
  28.     }
  29.  
  30. }
  31.  
  32.  
  33. proc SplitHelpValueForFile { file help value } {
  34.     upvar $help this_help
  35.     upvar $value this_value
  36.     
  37.     set thistext [ReadEntireFile "$file"]
  38.  
  39.     set in_help 1
  40.     
  41.     foreach temp_line [split $thistext \n]  {
  42.         if {[string index $temp_line 0] != "#"} {
  43.             set in_help 0
  44.         }
  45.         
  46.         if {$in_help == 1} {
  47.             append this_help "$temp_line\n"
  48.             #puts "H: $temp_line"
  49.         } else {
  50.             if { [string length $temp_line] > 0 } {
  51.                 append this_value "$temp_line\n"
  52.                 #puts "V: $temp_line"
  53.             }
  54.         }
  55.     }
  56.     
  57.     set this_value [string trim $this_value]
  58.     set this_help [string trim $this_help]
  59. }
  60.  
  61. # fills the global variables with the parsed values of all the config files
  62. proc FixAllfiles { old_dir new_dir dest_dir } {
  63.     puts "Upgrading config files from $old_dir to $new_dir, writing to $dest_dir";
  64.     set sh_config_files [GetFilesForDir $new_dir]
  65.     
  66.     foreach configfile $sh_config_files {
  67.         fix_file $configfile $old_dir $new_dir $dest_dir 
  68.     }
  69.     puts "Success!";
  70. }
  71.  
  72. # given a directory name, returns a list of files in that directory
  73. proc GetFilesForDir { dirname } {
  74.     set old_dir [pwd]
  75.     cd $dirname
  76.     set retval [glob -nocomplain *]
  77.     cd $old_dir
  78.     return $retval
  79. }
  80.  
  81. # read the entire contents of a file into memory and returns it
  82. proc ReadEntireFile { filename } {
  83.     #puts "reading file: $filename"
  84.     set file_text ""
  85.     catch {
  86.         set file_id [open $filename RDONLY]
  87.         set file_text [read $file_id]
  88.         close $file_id
  89.     }
  90.     return $file_text
  91. }
  92.  
  93. proc RemoveFile { remove_file keep_file dir } {
  94.  
  95.    # split the old and new files into help and data
  96.    set remove_help {}
  97.    set remove_value {}
  98.    set keep_help {}
  99.    set keep_value {}
  100.  
  101.    if { [file exists "$dir/$remove_file"] && \
  102.         [file exists "$dir/$keep_file"] } {
  103.     
  104.        SplitHelpValueForFile "$dir/$remove_file" remove_help remove_value
  105.        SplitHelpValueForFile "$dir/$keep_file" keep_help keep_value
  106.  
  107.        set merge_file [open "$dir/$keep_file" w]
  108.        puts $merge_file $keep_help
  109.        puts $merge_file $keep_value
  110.        puts $merge_file $remove_value
  111.        close $merge_file
  112.    }
  113.    
  114. }
  115.  
  116. proc main {} {
  117.     
  118.     puts "Working in: [pwd]"
  119.     #puts "Argv: $argv"
  120.     
  121.     #set old_dir "../config1"
  122.     #set new_dir "../config2"
  123.     #set dest_dir "../config3"
  124.     
  125.     set old_dir "cfgold"
  126.     set new_dir "config"
  127.     set dest_dir "config"
  128.  
  129.     # enumerate the files that have been reduced. 
  130.     #  a "reduction" means that some feature has been outmoded and it's 
  131.     #  function has been taken over by a "new" feature.
  132.     #
  133.     # thus, the data from the old feature must be included as part of 
  134.     #  the data in the "new" feature.
  135.     #
  136.     # format for array: index -> outmoded file
  137.     #                   value -> 'new' file
  138.     #
  139.     
  140.     array set remove [list banbody.txt bantext.txt]
  141.     
  142.     # perform reductions on any files that may need it.
  143.     
  144.     foreach old [array names remove] {
  145.       RemoveFile $old $remove($old) $old_dir
  146.     }
  147.  
  148.     FixAllfiles $old_dir $new_dir $dest_dir
  149. }
  150.  
  151. main
  152.